import re
import time
from selenium import webdriver
from selenium.common import NoSuchWindowException
from selenium.webdriver.common.by import By

name=[]
telephone=[]
Cell_phone=[]
fax=[]
address=[]
people=[]
class Albb:
    # 定义测试方法-账号不存在
    def login(self):
        driver = webdriver.Chrome()  # 创建浏览器驱动对象
        driver.maximize_window()  # 窗口最大化
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        options.add_argument('--disable-blink-features')
        options.add_argument('--disable-blink-features=AutomationControlled')  # 去除浏览器selenium监控
        # options.add_argument('--headless')  # 浏览器不提供可视化页面
        options.add_argument('--disable-gpu')  # 禁用GPU加速
        driver = webdriver.Chrome(options=options)
        driver.get("https://s.1688.com/selloffer/offer_search.htm?keywords=%BE%AB%C3%DC%BC%D3%B9%A4%BC%FE+cnc&spm=a26352.13672862.searchbox.input&beginPage=5#sm-filtbar") # 打开首页面
        driver.implicitly_wait(10)  # 隐式等待
        # 扫码登录
        # driver.find_element(By.XPATH,'//*[@id="login"]/div[1]/i').click()
        # time.sleep(20)
        # # 点击服务按钮
        # driver.find_element(By.XPATH,'//*[@id="topbar-box"]/div[2]/div/div[1]/div/div[2]').click()
        # time.sleep(2)
        # # 点击产品按钮
        # driver.find_element(By.XPATH,'//*[@id="topbar-box"]/div[2]/div/div[1]/div/div[3]/ul/li[2]/span').click()
        # time.sleep(2)
        # # 商品输入框
        # driver.find_element(By.XPATH,'//*[@id="topbar-box"]/div[2]/div/div[2]/input').send_keys('精密加工件 cnc')
        # time.sleep(2)
        # # 搜索按钮
        # driver.find_element(By.XPATH,'//*[@id="topbar-box"]/div[2]/div/button').click()
        # time.sleep(2)
        # 刷新页面
        driver.refresh()
        time.sleep(2)
        # 滚动鼠标
        driver.execute_script("window.scrollTo({}, {});".format(0, 500))
        time.sleep(2)
        driver.execute_script("window.scrollTo({}, {});".format(0, 1000))
        time.sleep(2)
        driver.execute_script("window.scrollTo({}, {});".format(0, 1500))
        time.sleep(2)
        driver.execute_script("window.scrollTo({}, {});".format(0, 2000))
        time.sleep(2)
        driver.execute_script("window.scrollTo({}, {});".format(0, 2500))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 3000))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 3500))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 4000))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 4500))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 5000))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 6000))
        time.sleep(3)
        driver.execute_script("window.scrollTo({}, {});".format(0, 5000))
        time.sleep(3)
        # 获取html数据
        content1=driver.page_source
        # 提取商家url
        pattern = r'tpCreditUrl":"(https://.*?\.1688\.com)'
        pattern1 = r'https://(.*?).1688.com'
        urls = re.findall(pattern, content1)
        for i in urls:
            resp = re.findall(pattern1, i)
            name.extend(resp)
        print(name)
        # for x in url_list:
        #     try:
        # # 执行JavaScript打开新的窗口
        #         driver.execute_script("window.open();")
        #         all_handles = driver.window_handles
        #         new_window_handle = [handle for handle in all_handles if handle != driver.current_window_handle][0]
        #         driver.switch_to.window(new_window_handle)
        #     #
        #     # # 获取所有窗口句柄
        #     # all_handles = driver.window_handles
        #
        #     # # 切换到新的窗口
        #     # new_window = [handle for handle in all_handles if handle != driver.current_window_handle][0]
        #     # driver.switch_to.window(new_window)
        #
        #         # 在新窗口中打开一个新的网址
        #         driver.get('https://{}.1688.com/page/contactinfo.htm?spm=0.0.wp_pc_common_topnav_38229151.0'.format(x))
        #         driver.refresh()
        #         time.sleep(10)
        #         merchant_name_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[2]'
        #         merchant_name=driver.find_element(By.XPATH,merchant_name_path).text
        #         print(merchant_name)
        #         name.append(merchant_name)
        #         merchant_telephone_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[1]/div[2]'
        #         merchant_telephone=driver.find_element(By.XPATH,merchant_telephone_path).text
        #         print(merchant_telephone)
        #         telephone.append(merchant_telephone)
        #         merchant_Cell_phone_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[2]/div[2]'
        #         merchant_Cell_phone=driver.find_element(By.XPATH,merchant_Cell_phone_path).text
        #         print(merchant_Cell_phone)
        #         Cell_phone.append(merchant_Cell_phone)
        #         merchant_fax_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[3]/div[2]'
        #         merchant_fax=driver.find_element(By.XPATH,merchant_fax_path).text
        #         print(merchant_fax)
        #         fax.append(merchant_fax)
        #         merchant_address_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[4]/div[2]'
        #         merchant_address=driver.find_element(By.XPATH,merchant_address_path)
        #         print(merchant_address)
        #         address.append(merchant_address)
        #         merchant_people_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[4]/div[3]'
        #         merchant_people=driver.find_element(By.XPATH,merchant_people_path)
        #         print(merchant_people)
        #         people.append(merchant_people)
        #         driver.refresh()
        #         driver.close()
        #         time.sleep(10)
        #         driver.switch_to.window(driver.window_handles[0])
        #         time.sleep(10)
        #     except NoSuchWindowException:
        #         print("新窗口未成功打开或已被关闭，跳过当前URL。")
        driver.quit()

        # 现在你可以在新窗口中执行其他的操作了
resp=Albb()
resp.login()
print(name)
print(telephone)
print(Cell_phone)
print(fax)
print(address)